package com.nbufe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbufe.common.constant.QueryPageParam;
import com.nbufe.common.constant.Result;
import com.nbufe.entity.CmsDevice;
import com.nbufe.entity.CmsLog;
import com.nbufe.service.CmsDeviceService;
import com.nbufe.service.CmsLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ylz.java
 * @since 2023-03-20
 */
@RestController
@RequestMapping("/ylz/cms-device")
public class CmsDeviceController {

    @Autowired
    private CmsDeviceService cmsDeviceService;
    @Autowired
    private CmsLogService cmsLogService;


    @GetMapping("/cms-device/list")
    @ApiOperation("查询所有的器件")
    public Result list() {
        cmsLogService.save(new CmsLog("管理员查询了所有器件"));
        return Result.suc(cmsDeviceService.list(null));
    }

    @GetMapping("/cms-device/id")
    @ApiOperation("根据id查询器件")
    public Result findById(@RequestParam String deviceid){
        cmsLogService.save(new CmsLog("管理员查询了器件id为" + deviceid + "的器件"));
        return Result.suc(cmsDeviceService.getById(deviceid));
    }


    @GetMapping("/cms-device/findByDeviceid")
    @ApiOperation("找数据库中是否存在该器件id的数据")
    public Result findByDeviceid(@RequestParam String deviceid) {
        QueryWrapper<CmsDevice> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("deviceid", deviceid);
        List list = cmsDeviceService.list(wrapperChapter);
        cmsLogService.save(new CmsLog("管理员查询了器件id为" + deviceid + "的器件"));
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    @PostMapping("/cms-device/save")
    @ApiOperation("新增一条器件")
    public Result save(@RequestBody CmsDevice cmsDevice) {
        cmsLogService.save(new CmsLog("管理员新增了一条器件"));
        return cmsDeviceService.save(cmsDevice) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-device/update")
    @ApiOperation("根据ID更新一条器件")
    public Result update(@RequestBody CmsDevice cmsDevice) {
        cmsLogService.save(new CmsLog("管理员更新了器件id为" + cmsDevice.getDeviceid() + "的器件"));
        return cmsDeviceService.updateById(cmsDevice) ? Result.suc() : Result.fail();
    }

    @ApiOperation("删除一条器件")
    @GetMapping("/cms-device/del")
    public Result del(@RequestParam String id) {
        cmsLogService.save(new CmsLog("管理员更新了器件id为" + id + "的器件"));
        return cmsDeviceService.removeById(id) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-device/listPage")
    @ApiOperation("分页模糊查询")
    public Result listPage(@RequestBody QueryPageParam query) {


        HashMap param = query.getParam();
        String type = (String) param.get("type");

        //当前页、每页多少条
        Page<CmsDevice> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<CmsDevice> wrapperChapter = new QueryWrapper<>();
        if (StringUtils.isNotBlank(type) && !"null".equals(type)) {
            wrapperChapter.like("type", type);
        }

        IPage result = cmsDeviceService.page(page, wrapperChapter);
        cmsLogService.save(new CmsLog("管理员分页查询了器件"));
        return Result.suc(result.getRecords(), result.getTotal());
    }
}

